package com.kaikeba.homework.spring.ioc;

import java.util.ArrayList;
import java.util.List;

/**
 * bean标签的信息
 */
public class BeanDefinition {
    /**
     * 类名称
     * bean标签的class属性
     */
    private String clazzName;
    /**
     * 类对象
     * 根据bean标签的class属性解析得到的Class对象
     */
    private Class<?> clazzType;
    /**
     * bean名称
     */
    private String beanName;
    /**
     * 类对象初始化方法，如果不指定，默认是构造方法
     */
    private String initMethod;
    /**
     * 默认是单例
     */
    private String scope = BeanScope.SINGLE;
    /**
     * bean中的属性信息
     */
    private List<PropertyValue> propertyValues = new ArrayList<>();

    public BeanDefinition(String clazzName, String beanName) {
        this.clazzName = clazzName;
        this.beanName = beanName;
        this.clazzType = resolveClassName(clazzName);
    }

    /**
     * 根据类路径解析对象类型
     *
     * @param clazzName 类完整路径
     * @return
     */
    private Class<?> resolveClassName(String clazzName) {
        try {
            return Class.forName(clazzName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean isSingleton() {
        return BeanScope.SINGLE.equals(this.scope);
    }

    public boolean isPrototype() {
        return BeanScope.MULTI.equals(this.scope);
    }

    public void addPropertyValue(PropertyValue propertyValue) {
        this.propertyValues.add(propertyValue);
    }

    // 以下为 get/set 方法
    // 略过...

    public String getClazzName() {
        return clazzName;
    }

    public void setClazzName(String clazzName) {
        this.clazzName = clazzName;
    }

    public Class<?> getClazzType() {
        return clazzType;
    }

    public void setClazzType(Class<?> clazzType) {
        this.clazzType = clazzType;
    }

    public String getBeanName() {
        return beanName;
    }

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public String getInitMethod() {
        return initMethod;
    }

    public void setInitMethod(String initMethod) {
        this.initMethod = initMethod;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public List<PropertyValue> getPropertyValues() {
        return propertyValues;
    }

    public void setPropertyValues(List<PropertyValue> propertyValues) {
        this.propertyValues = propertyValues;
    }
}
